#!/bin/bash
# Author    : some-guy (Muhammed U)
# Copyright : (C) 2008 some-guy
# E-mail    : muhammedu (at) gmail (dot) com

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# == display status
# args are printed
slax_print()
{ 
	echo "* $@"
}

# == bail from lzm install
# $@ == message
slax_bail()
{
	echo "ERROR:"
	echo "$@"
	echo "Exiting..."
	exit 1
}

# == check slapt-get
slax_checkslapt()
{
	[ -f /usr/sbin/slapt-get ] || echo "1"
}

# == prepare packages from tgz's using slapt-get
# $1  ==  slapt-get parameters (eg. "--install -d -p")
# $2  ==  slax-get temp dir (eg. /tmp/slax-get)
# $3  ==  slapt-get config (eg. /etc/slapt-get/slapt-getrc)
# $4+ ==  packages to install (eg. firefox thunderbird)
slax_makelzm()
{
	SLAPT_CONFIG="$3"
	SLAPT_ARGS="$1 --config=$SLAPT_CONFIG"
	SLAX_GET_TEMP="$2"
	USELESS_VAR=$@
	PACKAGES_INSTALL="$( (for one in `echo $1`; do export USELESS_VAR=`echo $USELESS_VAR | sed -e s,$one,,`; done; echo $USELESS_VAR) | sed -e s,$2,, -e s,$3,,)"
	[ "$PACKAGES_INSTALL" != "" ] || slax_bail "Packages must be provided"
	SLAPT_WORKING=`cat $SLAPT_CONFIG | grep WORKINGDIR | sed -e s/WORKINGDIR// -e s/=//`
	# == clean junk
	slapt-get --clean
	# == download tgz's
	slapt-get $SLAPT_ARGS $PACKAGES_INSTALL
	if [ $? -ne 0 ]; then
		exit 1
	else
		cd $SLAPT_WORKING
	fi
	# == move and extract tgz's
	TGZS=`find . -noleaf -type f | grep tgz`
	for t in $TGZS; do
		NAME=`echo $t | sed 's/.*\///g'`
		mkdir $SLAX_GET_TEMP/$NAME-packagebuild
		slax_print "Extracting $t"
		installpkg -root $SLAX_GET_TEMP/$NAME-packagebuild/ $t >/dev/null
		cd $SLAX_GET_TEMP/$NAME-packagebuild
		# == strip down
		find ./ -noleaf -type d | xargs chmod -v 755 &>/dev/null
		find . -noleaf | grep .gz | xargs gunzip &>/dev/null
		find . -noleaf | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded &>/dev/null
		cd $SLAPT_WORKING/
	done
	
	cd $SLAX_GET_TEMP
	for x in `ls -1 | grep packagebuild`; do
		# == generate modules
		pkgname=`echo -e $x | sed s/.tgz-packagebuild/-sg/`
		slax_print "Building Module: $pkgname"
		dir2lzm $x $HOME/Desktop/$pkgname.lzm >/dev/null
		rm -rf $x
		echo
	done
	cd $HOME
	rm -rf $TMP_DIR
}

# == config_file
# $1 == config location
slax_rc()
{
	LOCATION="$1"
	if [ ! -e "$LOCATION" ]; then
		echo -n "* Generating Config file"
		echo -n "."
		sleep 0.6
		echo "# See /usr/doc/slapt-get-0.9.12c/example.slapt-getrc " >$LOCATION
		echo "# for example source entries and configuration hints." >>$LOCATION
		echo "WORKINGDIR=/var/slapt-get" >>$LOCATION
		echo "EXCLUDE=^kernel-.*,^alsa-.*,^glibc.*,.*-[0-9]+dl$,^devs$,^udev$,aaa_elflibs,x86_64" >>$LOCATION
		echo "SOURCE=http://www.stabellini.net/filesystem/slackware-12.2" >>$LOCATION
		echo "SOURCE=http://darkstar.ist.utl.pt/slackware/addon/slacky/slackware-12.2/" >>$LOCATION
		echo "SOURCE=http://linuxpackages.cs.utah.edu/Slackware/Slackware-12.2-i386/" >>$LOCATION
		echo -n "."
		sleep 0.6
		echo "."
		sleep 0.6
		slax_print "Now run \"slax-get update\" to update the datebase, the config file is stored in $LOCATION"
	fi
}

# == activate modules
# $1 == module containing dir
slax_installlzm()
{
	MODULE_DIR="$1"
	# == activate modules
	cd $MODULE_DIR
	for e in `ls | grep -e -sg.lzm`; do
		echo -e "* Activating $e"
		activate $e >/dev/null
	done
}

# remove packages ===
# $1 == global module dir
# $2 == modules to remove
slax_removelzm()
{
	[ -n "$2" ] || slax_bail "Must provide packages"

	[ -d $1 ] || slax_bail "$1 doesn't exist"
	cd $1
	for r in $2; do
		FOUND=`find . -type f | grep $r`
		if [ ! -z $FOUND ]; then
			for t in $FOUND; do
				# == get basenames
				BASENAME=`basename $t`
				slax_print "Deactivating $BASENAME"
				deactivate $BASENAME >/dev/null
				slax_print "Removing $BASENAME"
				rm -rf $t
			done
		else
			slax_print "No Matches for $r"
		fi; done
}


# merge packages
# $* == packages to merge
slax_merge()
{
	if [ -z "$*" ]; then
		echo "Must provide packages"
		exit
	fi
		mkdir merge
		for k in $*; do
			echo "- Extracting $k"
			lzm2dir $k merge >/dev/null
		done
		echo "- Merging into merge.lzm"
		dir2lzm merge merge.lzm
		rm -rf merge
		echo
}
